package com.vainycos.lotterysearch.model.base;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import javax.lang.model.type.ErrorType;

@Data
public class Result<T> {

    /**
     * 请求是否成功
     */
    private Boolean isSuccess;

    /**
     * 失败编号
     */
    private String errCode;

    /**
     * 失败信息
     */
    private String errMessage;

    /**
     * 业务数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public Result() {

    }

    /**
     * 请求成功
     *
     * @return
     */
    public static Result buildSuccess() {
        Result result = new Result();
        result.setIsSuccess(true);
        return result;
    }

    /**
     * 请求失败
     *
     * @param errCode
     * @param errMessage
     * @return
     */
    public static Result buildFailure(String errCode, String errMessage) {
        Result result = new Result();
        result.setIsSuccess(false);
        result.setErrCode(errCode);
        result.setErrMessage(errMessage);
        return result;
    }

    /**
     * 成功后返回数据
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> of(T data) {
        Result<T> singleResponse = new Result();
        singleResponse.setIsSuccess(true);
        singleResponse.setData(data);
        return singleResponse;
    }
}
